/*
 *	Carcassonne
 *	2 ~ 6 Player
 *	1. Put init card
 *	2. For each player
 *		(1) Random pick a card and insert to init card
 *			If no place to put, skip it and pick it again
 *		(2) Design to put the resource on it or not
 *		(3) Scoring this round
 *	Insert rule:
 *		Every card have 4 sides,
 *		road should connected to road
 *		castle should connected to castle
 *		farm should connected to farm
 *	If no card, the game end
 */
#ifndef BASE_CARD_H
#define BASE_CARD_H

#include <stdio.h>
#include <stdlib.h>

#define INIT_CARD_INDEX (54)
#define TOTAL_CARD (72)

enum TERRAIN_TYPE
{
	TERRAIN_UNKNOWN = 0,
	TERRAIN_CASTLE,
	TERRAIN_FARM,
	TERRAIN_ROAD,
	TERRAIN_COUNT,
};

#define PLAYER_RESOURCE (7) // 8-1
// Base card type : 19
enum BASE_CARD_TYPE
{
	CARD_TYPE1 = 0, // x 5
	CARD_TYPE2,		// x 3
	CARD_TYPE3,		// x 3
	CARD_TYPE4,		// x 4
	CARD_TYPE5,		// x 2
	CARD_TYPE6,		// x 1
	CARD_TYPE7,		// x 9
	CARD_TYPE8,		// x 8
	CARD_TYPE9,		// x 4
	CARD_TYPE10,	// x 1
	CARD_TYPE11,	// x 4
	CARD_TYPE12,	// x 3
	CARD_TYPE13,	// x 2
	CARD_TYPE14,	// x 5
	CARD_TYPE15,	// x 4 (init card type)
	CARD_TYPE16,	// x 3
	CARD_TYPE17,	// x 3
	CARD_TYPE18,	// x 3
	CARD_TYPE19,	// x 5
	CARD_TYPE_COUNT, // total 72 pieces
};
enum ROTATING_CARD
{
	ROTATE_RIGHT_270 = -3,
	ROTATE_RIGHT_180,
	ROTATE_RIGHT_90,
	ROTATE_NONE,
	ROTATE_LEFT_90,
	ROTATE_LEFT_180,
	ROTATE_LEFT_270,
};
enum CARD_ORIENTATION
{
	ORIEN_N = 0,
	ORIEN_W,
	ORIEN_S,
	ORIEN_E,
};
enum TERRAIN_DIRECTION
{
	TOP = 0,
	RIGHT,
	BOTTOM,
	LEFT,
	TERRAIN_DIRECTION_COUNT,
};
struct card
{
	const int card_id; // 0 ~ 71
	const int card_type; // see BASE_CARD_TYPE
	int card_orien; // see CARD_ORIENTATION
	/*
	 *	How to get terrain_type from card structure?
		TERRAIN_CLOISTER,
	 *	terrain[(TERRAIN_TOP+card_orien)%TERRAIN_DIRECTION_COUNT]
	 */
	const int terrain_type[4]; // see TERRAIN_TYPE, use terrain_type() to get real type after rotating
	
	struct card *top, *right, *bottom, *left;
	const int castle_weighting;
	const int is_cloister;
	int x, y;
};
// Right rotate 90 degree card_orien - 1
// Left rotate 90 degree card_orien + 1
void card_rotate(int idx, int times);

int terrain_type(int idx, int direction);

struct card *get_card(int idx);

void set_card_pos(int idx, int x, int y);

void set_top(int target_id, int source_id);
void set_right(int target_id, int source_id);
void set_bottom(int target_id, int source_id);
void set_left(int target_id, int source_id);

int is_top_ok(int target_id , int source_id);
int is_right_ok(int target_id, int source_id);
int is_bottom_ok(int target_id, int source_id);
int is_left_ok(int target_id, int source_id);
#endif
